package com.lmc.gobang.config;

import com.lmc.gobang.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import com.lmc.gobang.Constants.constant;

/**
 * 用户登录拦截器
 */
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //从header中获取token
        String token = request.getHeader(constant.USER_TOKEN_HEADER);
        log.info("拦截器：userToken:"+token);
        //校验token
        Boolean flag = JwtUtil.checkToken(token);
        //成功则放行
        if (flag) {
            log.info("token校验成功，放行");
            return true;
        }
        response.setStatus(401);
        return false;
    }
}
